<?php
namespace app\validators;

use app\models\Member;
use yii\validators\Validator;

/**
 * 短信验证码验证器
 * Class SmscodeValidator
 */
class TjcodeValidator extends Validator
{
	/**
	 * 验证码过期时间
	 * @var int
	 */
	public $expireTime = 7*24*3600;

  /**
	 * @param \yii\base\Model $model
	 * @param string $attribute(tjcode)
	 */
	public function validateAttribute($model, $attribute)
	{
		$tjr = $model->tjr;
		$tjrmember = Member::find()->where([
			'name' => $tjr,
		])->one();

		/** @var $smsLog SmsLog */
		$time = time();
		$create_time=strtotime($tjrmember->push_tjm_create_time);
		if(
			is_null($tjrmember) ||
			($tjrmember->push_tjm != $model->$attribute)
		){
			$this->addError($model, $attribute,'推荐码错误');
		}else if($create_time > $time){
			$this->addError($model, $attribute,'推荐码异常，请重新向贝恩资本申请推荐码');
		}
		else if($time > ($create_time + $this->expireTime) )
		{
			$this->addError($model, $attribute,'推荐码已过期，请重新向贝恩资本申请推荐码');
		}else{
      return;
		}
	}

}
